package com.hbase.common;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;

/**
 * @desc:hbase数据链接
 * @author wangsh
 * @date 2017-12-20 下午8:15:36
 */
public class HBaseConncetion {

	private Configuration hbaseConf = null;

	private HConnection hbaseConn = null;

	/**
	 * @Description:获取Configuration实例
	 * @return
	 * @return Configuration 返回类型
	 * @throws
	 */
	public synchronized Configuration getHBaseConfiguration() {
		if (hbaseConf == null) {
			hbaseConf = HBaseConfiguration.create();
		}
		return hbaseConf;
	}

	/**
	 * @Description:获取HConnection实例
	 * @return
	 * @throws IOException
	 * @return HConnection 返回类型
	 * @throws
	 */
	public synchronized HConnection getHBaseConnection() throws IOException {
		if (hbaseConn == null) {
			hbaseConn = HConnectionManager.createConnection(getHBaseConfiguration());
		}
		return hbaseConn;
	}

	/**
	 * @Description:获取表连接
	 * @return
	 * @throws IOException
	 * @return HBaseAdmin 返回类型
	 * @throws
	 */
	public synchronized HBaseAdmin getHBaseAdmin() throws IOException {
		HBaseAdmin hbaseAdmin = new HBaseAdmin(getHBaseConfiguration());
		return hbaseAdmin;
	}

	/**
	 * 
	 * @Description:关闭资源
	 * @date:Dec 21, 2017 2:25:13 PM
	 * @throws IOException
	 * @return void 返回类型
	 * @throws
	 */
	public void close() throws IOException {
		if (hbaseConn != null && !hbaseConn.isClosed()) {
			hbaseConn.close();
		}
	}

}
